/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.component;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.Iterator;
import java.util.Map;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.ruipengkj.commons.util.Util;
import com.ruipengkj.factory.ImageFactory;
/**
 * 说明：
 * 文本+下拉组件
 * @Author：李智深
 * @Date：2012-03-14
 */
public class JLabelJComboBoxComponent extends JPanel{
	
	private static final long serialVersionUID = 1L;
	private JComboBox box;
	private DefaultComboBoxModel model;
	private Dimension dimension=null;
	private ImageIcon icon=ImageFactory.loadImage(ImageFactory.ICON_4);
	private Map<?, ?> map;
	public JLabelJComboBoxComponent(String lable,String[] data){
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.init(lable,data);
		box=new JComboBox(model);
		this.add(box);
		
		this.setCursor(Util.CURSOR_DEFAULT);
		box.setCursor(Util.CURSOR_DEFAULT);
	}
	
	public JLabelJComboBoxComponent(String lable,String[] data,int width){
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.init(lable,data);
		dimension=new Dimension(width, 25);
		box=new JComboBox(model);
		this.setBoxSize(dimension);
		this.add(box);
		
		this.setCursor(Util.CURSOR_DEFAULT);
		box.setCursor(Util.CURSOR_DEFAULT);
	}
	
	public JLabelJComboBoxComponent(String lable,String[] data,int width, Component comp){
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.init(lable,data);
		dimension=new Dimension(width, 25);
		box=new JComboBox(model);
		this.setBoxSize(dimension);
		this.add(box);
		this.add(comp);
		
		this.setCursor(Util.CURSOR_DEFAULT);
		box.setCursor(Util.CURSOR_DEFAULT);
	}
	
	public JLabelJComboBoxComponent(String isRed, String lable,String[] data,int width){
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		JLabel red = new JLabel(isRed,icon,JLabel.LEFT);
		red.setForeground(Color.red);
		this.add(red);
		this.setOpaque(false);
		JLabel label=new JLabel(lable);
		label.setVerticalAlignment(JLabel.TOP);
		model=new DefaultComboBoxModel();
		if(data!=null&&data.length!=0){
			for(int i=0;i<data.length;i++){
				model.addElement(data[i]);
			}
		}else{
			data=new String[]{};
		}
		this.add(label);
		dimension=new Dimension(width, 25);
		box=new JComboBox(model);
		this.setBoxSize(dimension);
		this.add(box);
		
		this.setCursor(Util.CURSOR_DEFAULT);
		box.setCursor(Util.CURSOR_DEFAULT);
	}
	
	public JLabelJComboBoxComponent(String lable,Map<Object, ?> map){
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.map=map;
		this.init(lable,map);
		box=new JComboBox(model);
		this.add(box);
		
		this.setCursor(Util.CURSOR_DEFAULT);
		box.setCursor(Util.CURSOR_DEFAULT);
	}
	
	public JLabelJComboBoxComponent(String lable,Map<?, ?> map,int width){
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.map=map;
		this.init(lable,map);
		dimension=new Dimension(width, 25);
		box=new JComboBox(model);
		this.setBoxSize(dimension);
		this.add(box);
		
		this.setCursor(Util.CURSOR_DEFAULT);
		box.setCursor(Util.CURSOR_DEFAULT);
	}
	
	public JLabelJComboBoxComponent(String lable,Map<?, ?> map,int width, int height){
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.map=map;
		this.init(lable,map);
		dimension=new Dimension(width, height);
		box=new JComboBox(model);
		this.setBoxSize(dimension);
		this.add(box);
		
		this.setCursor(Util.CURSOR_DEFAULT);
		box.setCursor(Util.CURSOR_DEFAULT);
	}
	
	private void init(String lable,String[] data){
		this.setOpaque(false);
		JLabel label=new JLabel(lable,icon,JLabel.LEFT);
		label.setVerticalAlignment(JLabel.TOP);
		model=new DefaultComboBoxModel();
		if(data!=null&&data.length!=0){
			for(int i=0;i<data.length;i++){
				model.addElement(data[i]);
			}
		}else{
			data=new String[]{};
		}
		this.add(label);
	}
	private void init(String lable,Map<?, ?> mapByte){
		this.setOpaque(false);
		JLabel label=new JLabel(lable,icon,JLabel.LEFT);
		label.setVerticalAlignment(JLabel.TOP);
		model=new DefaultComboBoxModel();
		if(mapByte!=null&&mapByte.size()!=0){
			Iterator<?> it=mapByte.keySet().iterator();
			while(it.hasNext()){
				Object key=it.next();
				model.addElement(mapByte.get(key));
			}
		}
		this.add(label);
	}
	
	private void setBoxSize(Dimension dimension){
		box.setMinimumSize(dimension);
		box.setMaximumSize(dimension);
		box.setSize(dimension);
		box.setPreferredSize(dimension);
	}
	
	public Object getKeyByVaule(String value){
		if(map!=null&&map.size()!=0){
			Iterator<?> it=map.keySet().iterator();
			while(it.hasNext()){
				Object key=it.next();
				if(map.get(key).equals(value)){
					return key;
				}
			}
		}
		return 0;
	}
	
	public JComboBox getBox() {
		return box;
	}
	public void setBox(JComboBox box) {
		this.box = box;
	}
	public DefaultComboBoxModel getModel() {
		return model;
	}
	public void addElement(Object anObject){
		model.addElement(anObject);
	}
	public void remove(){
		model.removeAllElements();
	}
}
